package com.gitee.feizns.ding.talk.zzd;

import com.alibaba.xxpt.gateway.shared.api.request.OapiMoziEmployeeGetByMobileRequest;
import com.alibaba.xxpt.gateway.shared.api.request.OapiRpcOauth2DingtalkAppUserJsonRequest;
import com.alibaba.xxpt.gateway.shared.api.response.OapiMoziEmployeeGetByMobileResponse;
import com.alibaba.xxpt.gateway.shared.api.response.OapiRpcOauth2DingtalkAppUserJsonResponse;
import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.alibaba.xxpt.gateway.shared.client.http.IntelligentGetClient;
import com.alibaba.xxpt.gateway.shared.client.http.IntelligentPostClient;
import com.alibaba.xxpt.gateway.shared.client.http.api.OapiSpResultContent;
import com.gitee.feizns.quickstart.jackson.Val;
import lombok.Getter;

/**
 * 浙政钉工具类
 * @author feizns
 * @since 2025/04/06
 */
public class ZzdUtils {

    /**
     * 配置
     */
    @Getter
    private final ZzdConfig config;

    /**
     * {@link ExecutableClient}
     */
    @Getter
    private final ExecutableClient client;

    /**
     * 访问令牌
     */
    private final AccessToken accessToken;

    /**
     * zzd 实用程序
     * @param config 配置
     */
    public ZzdUtils(ZzdConfig config) {
        this.config = config;
        this.client = createClient(config);
        this.accessToken = new AccessToken(this);
    }

    /**
     * 根据authCode换取用户信息
     * <a href="https://open-portal.on-premises.dingtalk.com/portal/#/helpdoc?apiType=serverapi&docKey=2674834">详情</a>
     * @return {@link Val }
     */
    public Val getUserinfoByAuthCode(String code) {
        //executableClient保证单例
        IntelligentPostClient intelligentPostClient = client.newIntelligentPostClient("/rpc/oauth2/dingtalk_app_user.json");
        OapiRpcOauth2DingtalkAppUserJsonRequest oapiRpcOauth2DingtalkAppUserJsonRequest = new OapiRpcOauth2DingtalkAppUserJsonRequest();
        //登录access_token
        oapiRpcOauth2DingtalkAppUserJsonRequest.setAccess_token(accessToken.get());
        //临时授权码
        oapiRpcOauth2DingtalkAppUserJsonRequest.setAuth_code(code);
        //获取结果
        OapiRpcOauth2DingtalkAppUserJsonResponse apiResult = intelligentPostClient.post(oapiRpcOauth2DingtalkAppUserJsonRequest);
        return Utils.getContentIfFailThrow(apiResult);
    }

    /**
     * 根据手机号码获取人员编码
     * <a href="https://open-portal.on-premises.dingtalk.com/portal/#/helpdoc?apiType=serverapi&docKey=2674906">详情</a>
     * @return {@link Val }
     */
    public OapiSpResultContent getEmployeeByMobile(String mobile) {
        //executableClient保证单例
        IntelligentGetClient intelligentGetClient = client.newIntelligentGetClient("/mozi/employee/get_by_mobile");
        OapiMoziEmployeeGetByMobileRequest oapiMoziEmployeeGetByMobileRequest = new OapiMoziEmployeeGetByMobileRequest();
        //手机区号
        oapiMoziEmployeeGetByMobileRequest.setAreaCode("86");
        //租户ID
        oapiMoziEmployeeGetByMobileRequest.setTenantId(config.getTenantId());
        //账号类型
        oapiMoziEmployeeGetByMobileRequest.setNamespace("local");
        //手机号
        oapiMoziEmployeeGetByMobileRequest.setMobile(mobile);
        //获取结果
        OapiMoziEmployeeGetByMobileResponse apiResult = intelligentGetClient.get(oapiMoziEmployeeGetByMobileRequest);
        return Utils.getContentIfFailThrow(apiResult);
    }

    /**
     * 创建客户端
     * @param config 配置
     * @return {@link ExecutableClient }
     */
    public static ExecutableClient createClient(ZzdConfig config) {
        ExecutableClient client = ExecutableClient.getInstance();
        client.setDomainName(config.getDomainName());
        client.setProtocal(config.getProtocal());
        client.setAccessKey(config.getAccessKey());
        client.setSecretKey(config.getSecretKey());
        client.init();
        return client;
    }

}
